import { BasicColumn, FormSchema } from '@/components/Table';
import { getAdminModule } from '@/api/system/account';
import { find } from 'lodash-es';

export const columns: BasicColumn[] = [
  {
    title: '日志编号',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '操作账号',
    dataIndex: 'operator_name',
  },
  {
    title: '模块',
    dataIndex: 'module_name',
  },
  {
    title: '子模块',
    dataIndex: 'menu_name',
  },
  {
    title: '操作IP',
    dataIndex: 'ip',
  },
  {
    title: '操作详情',
    dataIndex: 'remark',
  },
  {
    title: '操作时间',
    dataIndex: 'created_at',
  },
];
let aModelList = [];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'operator_name',
    label: '操作账号',
    component: 'Input',
  },
  {
    field: 'ip',
    label: '操作ip',
    component: 'Input',
  },
  {
    label: '模块名称',
    field: 'module_id',
    component: 'ApiSelect',
    componentProps: ({ formModel, formActionType }) => {
      return {
        api: getAdminModule,
        labelField: 'title',
        valueField: 'id',
        resultField: 'list',
        onOptionsChange: (options) => {
          aModelList = options;
        },
        onChange: (e: any) => {
          let citiesOptions = [];
          if (find(aModelList, { value: e })) {
            // @ts-ignore
            citiesOptions = find(aModelList, { value: e })['sub'] || [];
          }
          formModel.menu_id = undefined; //  reset city value
          const { updateSchema } = formActionType;
          updateSchema({
            field: 'menu_id',
            componentProps: {
              options: citiesOptions,
              fieldNames: {
                label: 'title',
                value: 'id',
              },
            },
          });
        },
      };
    },
    colProps: {
      span: 5,
    },
  },
  {
    label: '子模块',
    field: 'menu_id',
    component: 'Select',
    colProps: {
      span: 5,
    },
  },
  {
    field: '[start_time, end_time]',
    label: '操作时间',
    component: 'RangePicker',
    componentProps: {
      format: 'YYYY-MM-DD HH:mm:ss',
      placeholder: ['开始时间', '结束时间'],
      showTime: { format: 'HH:mm:ss' },
    },
  },
];
